package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;

import biz.FoodBiz;
import biz.FoodSizeBiz;
import biz.FoodTypeBiz;
import biz.RateBiz;
import biz.impl.FoodBizImpl;
import biz.impl.FoodSizeBizImpl;
import biz.impl.FoodTypeBizImpl;
import biz.impl.RateBizImpl;
import entity.Food;
import entity.FoodSize;
import entity.FoodType;
import entity.Rate;
import servlet.util.FileSaveToDsk;
import servlet.util.FileUploadRequestParser;

@WebServlet("/admin/AdminFoodAddServlet.html")
public class AdminFoodAddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		FoodTypeBiz foodTypeBiz= new FoodTypeBizImpl();
		List<FoodType> allFoodType = foodTypeBiz.queryAllFoodType();
		//foodType下拉框
		request.setAttribute("allFoodType", allFoodType);
		request.getRequestDispatcher("adminFoodAdd.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//食物名字
		FileUploadRequestParser parser = new FileUploadRequestParser(request);
		String foodName = parser.getFormFileIdValue("foodName");
		//获取食物类型
		String foodType = parser.getFormFileIdValue("foodTypeSelect");
		Integer ftId = Integer.parseInt(foodType);
		FoodTypeBiz foodTypeBiz = new FoodTypeBizImpl();
		FoodType fType = foodTypeBiz.queryFoodTypeByFoodTypeId(ftId);
		//食物价格
		String foodPrice = parser.getFormFileIdValue("foodPrice");
		double fPrice = Double.valueOf(foodPrice);
		//食物详情
		String foodDesc = parser.getFormFileIdValue("foodDesc");
		//获取图片文件
		FileItem fileFieId = parser.getFileFieId("imgPath");
		//存盘
		FileSaveToDsk fstd = new FileSaveToDsk(request);
		fstd.FileSave(fileFieId, "/image/foods");
		String clientName =fstd.getClientName();//食物图片路径
		//添加食物
		FoodBiz foodBiz = new FoodBizImpl();
		foodBiz.addFood(new Food(-1,fType,foodName,foodDesc,clientName,fPrice));
		
		
		//Rate费用表添加foodSize 的比率
		String small = parser.getFormFileIdValue("small");
		String middle = parser.getFormFileIdValue("middle");
		String big = parser.getFormFileIdValue("big");
		double smallRate = Double.valueOf(small);
		double middleRate = Double.valueOf(middle);
		double bigRate = Double.valueOf(big);
		//获取foodSize对象
//		FoodSizeBiz foodSizeBiz = new FoodSizeBizImpl();
//		FoodSize queryFoodSizeByFoodSizeId = foodSizeBiz.queryFoodSizeByFoodSizeId(1);
		
		
		//Rate费用表添加foodSize 的Id
		RateBiz rateBiz = new RateBizImpl();
		//allfood 取最后一个
		List<Food> queryAllFood = foodBiz.queryAllFood();
		Food food = queryAllFood.get(queryAllFood.size()-1);
		//取foodSize
		System.out.println(food.getfId());
		FoodSize fb1 = new FoodSize(4,null);
		FoodSize fb2 = new FoodSize(5,null);
		FoodSize fb3 = new FoodSize(6,null);
		rateBiz.addRate(new Rate(8, food, fb1, smallRate));
		rateBiz.addRate(new Rate(8, food, fb2, middleRate));
		rateBiz.addRate(new Rate(8, food, fb3, bigRate));
		//添加成功
		response.sendRedirect("AdminFoodServlet.html");
	}

}
